package com.rh.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.entity.CrmBanner;
import com.rh.service.CrmBannerService;
import com.rh.result.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 首页banner表 前端控制器
 */
@Api(tags = "前端管理")
@RestController
@RequestMapping("/cmsservice/banneradmin")
public class CrmBannerController {
    @Autowired
    private CrmBannerService crmBannerService;
    /**
     * 1.分页查询banner列表
     */
    @GetMapping("pageBanner/{page}/{limit}")
    @ApiOperation(value = "1.获取前端Banner分页列表")
    public ResultVo pageBanner(@PathVariable Long page,
                               @PathVariable Long limt){
     Page<CrmBanner>crmBannerPage=new Page<>();
     crmBannerService.page(crmBannerPage, null);
     return ResultVo.ok().data("items",crmBannerPage.getRecords()).data("total",crmBannerPage.getTotal());
    }
    /**
     * 2.添加Banner
     */
    @PostMapping("addBanner")
    @ApiOperation(value = "2.新增Banner")
    public ResultVo addBanner(@RequestBody CrmBanner crmBanner){
        boolean save = crmBannerService.save(crmBanner);
        if(save){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
    /**
     * 3.通过id获取Banner信息
     */
    @GetMapping("get/{id}")
    @ApiOperation(value = "3.根据id获取Banner信息")
    public  ResultVo getBannerById(@PathVariable String id){
        CrmBanner byId = crmBannerService.getById(id);
        return ResultVo.ok().data("item",byId);
    }
    /**
     * 4.修改banner信息
     */
    @PutMapping("update")
    @ApiOperation(value = "4.修改Banner信息")
    public ResultVo updateBanner(@RequestBody CrmBanner crmBanner){
        boolean b = crmBannerService.updateById(crmBanner);
        if(b){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
    /**
     * 5.删除Banner信息
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "5.删除Banner信息")
    public ResultVo deleteBanner(@PathVariable String id){
        boolean b = crmBannerService.removeById(id);
        if(b){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
}

